package com.feisi.dao;

import com.feisi.dao.sqlProvider.UserSqlProvider;
import com.feisi.domain.User;
import org.apache.ibatis.annotations.*;
import org.springframework.stereotype.Repository;

import java.util.List;

@Repository
public interface UserDao {

    /**
     * 查询所有user(分页)
     * @param page
     * @param limit
     * @return
     */
    @Select("select * from user limit #{page},#{limit}")
    List<User> findAll(@Param("page") Integer page, @Param("limit") Integer limit);

    /**
     * 查询总条数
     * @return
     */
    @Select("select count(*) from user")
    Integer usersTotal();


    /**
     * 根据用户名搜索用户
     * @param u_name
     * @param page
     * @param limit
     * @return
     */
    @Select("select * from user where u_name = #{u_name} limit #{page},#{limit}")
    List<User> findByName(@Param("u_name") String u_name, @Param("page") Integer page, @Param("limit") Integer limit);

    /**
     * 查询总条数,根据用户名
     * @param u_name
     * @return
     */
    @Select("select count(*) from user where u_name = #{u_name}")
    Integer usersTotalByName(@Param("u_name") String u_name);

    /**
     * 根据id查询用户
     * @param id
     * @return
     */
    @Select("select * from user where id=#{id}")
    User findUserById(@Param("id") Integer id);

    /**
     * 根据用户(员工)部门查找用户
     * 实现通讯录模板的 选择部门 功能
     * @param u_department
     * @param page
     * @param limit
     * @return
     */
    @Select("select * from user where u_department=#{u_department} limit #{page},#{limit}")
    List<User> findByDepartment(@Param("u_department") String u_department, @Param("page") Integer page, @Param("limit") Integer limit);

    /**
     * 根据用户(员工)部门查找用户数
     * 实现通讯录模板的 选择部门 功能
     * @param u_department
     * @return
     */
    @Select("select count(*) from user where u_department=#{u_department}")
    Integer userTotalByDepartment(@Param("u_department") String u_department);

    /**
     * 新增用户
     *
     * @param user
     */
    @Insert("insert into user values(#{id},#{u_name},#{u_password},#{u_phone},#{u_doc_type},#{u_doc_number}," +
            "#{u_sex},#{u_age},#{u_birthday},#{u_ismarry},#{u_country},#{u_nation},#{u_politics},#{u_native}," +
            "#{u_health},#{u_education},#{u_job_number},#{u_department},#{u_entry_time},#{u_job},#{u_superior})")
    void saveUser(User user);

    /**
     * 修改用户
     * @param user
     */
    @Update("update user set id=#{id},u_name=#{u_name},u_password=#{u_password},u_phone=#{u_phone},u_doc_type=#{u_doc_type},u_doc_number=#{u_doc_number}," +
            "u_sex=#{u_sex},u_age=#{u_age},u_birthday=#{u_birthday},u_ismarry=#{u_ismarry},u_country=#{u_country},u_nation=#{u_nation},u_politics=#{u_politics},u_native=#{u_native}," +
            "u_health=#{u_health},u_education=#{u_education},u_job_number=#{u_job_number},u_department=#{u_department},u_entry_time=#{u_entry_time},u_job=#{u_job},u_superior=#{u_superior}")
    void updateUser(User user);


    /**
     * 修改密码
     * @param id
     * @param password
     */
    @Update("update user set u_password=#{password} where id=#{id}")
    void updateUserPassword(@Param("id") Integer id, @Param("password") String password);


    /**
     * 根据用户名和密码查询用户
     * 登录注册时用到
     * @param username
     * @param password
     * @return
     */
    @Select("select * from user where u_name=#{username} and u_password=#{password}")
    List<User> findByNameAndPassword(@Param("username") String username, @Param("password") String password);


    /**
     * 根据id删除用户
     * 批量删除
     * @param ids
     */
    @DeleteProvider(type = UserSqlProvider.class,method = "deleteByIds")
    void deleteUser(@Param("ids") Integer[] ids);
}
